module controller.TeleplayVlinkController;

import std.conv : to;
import std.json : JSONValue, parseJSON;

import archttp.HttpStatusCode : HttpStatusCode;
import archttp.HttpRequest : HttpRequest;
import archttp.HttpResponse : HttpResponse;

import model.TeleplayVlink;
import repository.TeleplayLinksRepo;
import utils.RespData : RespData;
import utils.ObjMapping : ObjMapping;
import utils.SecurityVerification : signValidator;

/** 
 * 电视剧视频链接控制器
 */
class TeleplayVlinkController
{

    TeleplayLinksRepo _tlrepo; // TeleplayLinks仓储
    this ()
    {
        // 注入TeleplayLinks仓储
        _tlrepo = new TeleplayLinksRepo();
    }

    /** 
     * 获取一个视频链接
     */
    public void getOne (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);
        auto tlink = _tlrepo.getOneTeleplayLink(id);

        if (tlink is null)
        {
            resp.code(HttpStatusCode.NOT_FOUND).json(RespData.newData(404, "未找到")).send();
            return;
        }

        JSONValue respData = RespData.newData!(TeleplayVlinkDto)(200, "ok", ObjMapping.mappingToObject!(
                TeleplayVlinkDto, TeleplayVlink)(
                tlink));
        resp.json(respData).send();
    }

    /** 
     * 添加链接
     */
    public void addLink (HttpRequest req, HttpResponse resp)
    {
        JSONValue json = parseJSON(req.body());
        string teleplay_id = json["teleplay_id"].str;
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        TeleplayVlinkDtoFromCreate tl = new TeleplayVlinkDtoFromCreate(teleplay_id, name, url, real_url);

        bool res = _tlrepo.addTeleplayLink(ObjMapping.mappingToObject!(TeleplayVlink, TeleplayVlinkDtoFromCreate)(
                tl));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.code(HttpStatusCode.CREATED).json(RespData.newData(200, "ok")).send();
    }

    /** 
     * 删除一个链接
     */
    public void removeLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        string sign = req.query("sign");
        if (idStr == "" || sign == "" || sign is null)
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);
        if (!signValidator(idStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        bool res = _tlrepo.removeTeleplayLink(id);
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.code(HttpStatusCode.CREATED).json(RespData.newData(200, "ok")).send();
    }

    /** 
     * 编辑链接
     */
    public void editLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);

        JSONValue json = parseJSON(req.body());
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        TeleplayVlinkDtoFromUpdate tl = new TeleplayVlinkDtoFromUpdate(name, url, real_url);

        bool res = _tlrepo.editTeleplayLink(id, ObjMapping.mappingToObject!(TeleplayVlink, TeleplayVlinkDtoFromUpdate)(
                tl));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.code(HttpStatusCode.CREATED).json(RespData.newData(200, "ok")).send();
    }
}
